home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 38 / sgn_bans.zip / BRESENHM.PAS < prev    next >
Pascal/Delphi Source File  |  1985-10-28  |  2KB  |  84 lines

  1. { BRESENHM.PAS  ( note, not BresenhAm.Pas ) }
  2. {                                  ~        }
  3.  
  4. { Module is for use with Epson graphics package Printpak.Pas }
  5.  
  6. { Bresenham's algorithm from drawing a pixel line }
  7. { Byte, Nov. 1985, pp 219-232 }
  8. { Pascal version by Prof. Richard Rasala, Northeastern U. }
  9.  
  10. Procedure Bresenham(x1,y1,x2,y2 : integer ) ;
  11.  
  12. Var
  13.   x ,y, z, a, b, dx, dy, d, deltap, deltaq : integer ;
  14.  
  15. begin
  16.   dx := abs(x2-x1) ;
  17.   dy := abs(y2-y1) ;
  18.   if dy <= dx then         { slope <= 1 }
  19.   begin
  20.     x := x1 ; { initialize x }
  21.     y := y1 ; { initialize y }
  22.     z := x2 ; { set sentinel in x direction }
  23.     { now set x-increment ...}
  24.     if x1 <= x2
  25.       then a := 1     { x increases }
  26.       else a := -1 ;  { x decreases }
  27.     { now set y increment ...}
  28.     if y1 <= y2
  29.       then b := 1    { y increases }
  30.       else b := -1 ; { y decreases }
  31.     { initialize decision function d and its deltas }
  32.     { note, u & v are Bresenham's own notation }
  33.     deltap := dy + dy ;    { 2v }
  34.     d := deltap - dx ;     { 2v - u is initial value of d }
  35.     deltaq := d - dx ;     { 2v - 2u }
  36.     { locate and plot points ...}
  37.     Pset(x,y,1) ; { first point }
  38.     while x <> z do
  39.     begin
  40.       x := x + a ;            { always increment x }
  41.       if d < 0
  42.         then d := d + deltap  { d = d + 2v }
  43.         else
  44.         begin
  45.           y := y + b ;        { increment y also - i.e. go diagonal }
  46.           d := d + deltaq ;   { d = d + 2v - 2u }
  47.         end ; { else }
  48.       Pset(x,y,1) ;
  49.     end ; { while }
  50.   end { case of dy <= dx }
  51.   else { dx <= dy so view x as a function of y }
  52.   begin
  53.     y := y1 ; { initialize y }
  54.     x := x1 ; { initialize x }
  55.     z := y2 ; { set sentinel in y direction }
  56.     { now set y increment ...}
  57.     if y1 <= y2
  58.       then a := 1    { y increase }
  59.       else a := -1 ; { y decreases }
  60.     { now set x increment }
  61.     if x1 <= x2
  62.       then b := 1    { x increase }
  63.       else b := -1 ; { x decreases }
  64.     { initialize decision function and its deltas }
  65.     deltap := dx + dx ;
  66.     d := deltap - dy ;
  67.     deltaq := d - dy ;
  68.     { locate and plot points }
  69.     Pset(x,y,1) ; { first point }
  70.     while y <> z do
  71.     begin
  72.       y := y + a ;
  73.       if d < 0
  74.         then d := d + deltap
  75.         else
  76.         begin
  77.           x := x + b ;
  78.           d := d + deltaq ;
  79.         end ; { else }
  80.     Pset(x,y,1) ;
  81.     end { while }
  82.   end { else }
  83. end ; { Bresenham }
  84.